﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Runtime;
using BneyBaruch.Ingest.MorningLesson.Services.Data;
using BneyBaruch.Ingest.MorningLesson.Services;
using BneyBaruch.Ingest.Core.ServiceModel;
using System.ServiceModel;

namespace BneyBaruch.Ingest.MorningLesson.WorkflowCommunication.RecordingController
{
    public class RecordingControllerExchangeService : IRecordingControllerExchangeService,IDisposable
    {
        #region Constants

        private const string RecordingControllerEndpointConfigurationName = "RecordingControllerEndpoint";

        #endregion

        #region Fields

        private WorkflowRuntime workflowRuntime;
        private RecordingControllerCallback recordingControllerCallback;
        private DuplexServiceProxy<IRecordingController> recordingController = null;

        #endregion

        #region Constructors

        public RecordingControllerExchangeService(WorkflowRuntime workflowRuntime)
        {
            this.workflowRuntime = workflowRuntime;
            recordingControllerCallback = new RecordingControllerCallback(this);
            recordingController = new DuplexServiceProxy<IRecordingController>(
                new InstanceContext(recordingControllerCallback),  // Instance context for callback duplex communication
                RecordingControllerEndpointConfigurationName // Endpoint configuration name
            );
        }

        #endregion

        #region IRecordingControllerExchangeService Events

        public event EventHandler<MatrixAdjustmentApprovedEventArgs> MatrixAdjustmentApproved;
        public event EventHandler<FileSystemPreparationReportEventArgs> FileSystemOnRecordingControllerReadyToUse;

        #endregion

        #region IRecordingControllerExchangeService Matrix Adjustment Methods

        public void NotifyMatrixAdjustmentApproved(Guid workflowInstanceId)
        {
            MatrixAdjustmentApprovedEventArgs e = new MatrixAdjustmentApprovedEventArgs(workflowInstanceId);

            if (MatrixAdjustmentApproved != null)
            {
                MatrixAdjustmentApproved(null, e);
            }
        }

        public void RequestMatrixAdjustment(Guid workflowInstanceId)
        {
            recordingController.Proxy.RequestMatrixAdjustment(
                new RequestMatrixAdjustmentRequest()
                {
                    WorkflowInstanceId = workflowInstanceId
                }
            );
        }

        #endregion

        #region IRecordingControllerExchangeService File System Prepare Before Recording Methods

        public void NotifyFileSystemBeforeRecordingCommandCompletedOnRecordingController(Guid workflowInstanceId)
        {
            FileSystemPreparationReportEventArgs e = new FileSystemPreparationReportEventArgs(workflowInstanceId);

            if (FileSystemOnRecordingControllerReadyToUse != null)
            {
                FileSystemOnRecordingControllerReadyToUse(null, e);
            }
        }

        public void PrepareFileSystemBeforeRecordingOnRecordingController(Guid workflowInstanceId)
        {
            recordingController.Proxy.PrepareFileSystemBeforeRecording(
                new PrepareFileSystemBeforeRecordingRequest()
                {
                    WorkflowInstanceId = workflowInstanceId
                }
            );
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (recordingController != null)
                recordingController.Dispose();
        }

        #endregion
    }
}
